/* $Id: daemon.c 17 2007-01-02 17:31:52Z judas $ */

#include <nih/cdefs.h>

#include <nih/daemon.h>

#include <stdlib.h>
#include <stdio.h>
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STAT_H
#include <stat.h>
#endif

inline int nih_daemonize()
{
	if (fork() != 0)
		exit(EXIT_SUCCESS);

	setsid();

	if (fork() != 0)
		exit(EXIT_SUCCESS);

	chdir("/");
	umask(0);

	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);

        return 0;
}

